<? 
	include('base.php');
	User::protect();
	include_class('band_members');
	include_class('m2');
	$section = 'band_members';
	$type = ($_GET['type'] == 'av') ? 'av' : 'photos';
	$bm = BandMember::get($_GET['id']);
	
	if (!db::isError($bm)) {
		$ma = ($type == 'av') ? $bm->getAVAreaObject() : $bm->getPhotoAreaObject();	
	
		switch($_GET['task']) {
			case 'register_media':
				$res = $bm->registerMedia($_GET['type']);				
				if (!db::isError($res)) {
					header('Location: band_member_media.php?id=' . $_GET['id'] . '&type=' . $type);
				}
				break;
			case 'rescan':
				if (!db::isError($ma)) {
					$res = $ma->rescan($bm);
					if (!db::isError($res)) {
						header('Location: band_member_media.php?id=' . $_GET['id']);
					}
				}
				break;
			case 'move_up':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$type = ($mi->getAreaID() == $bm->getAVAreaID()) ? "av" : "photos";
						$res = $mi->moveUp($bm);
						if (!db::isError($res)) {
							header('Location: band_member_media.php?id=' . $_GET['id'] . '&type=' . $type);
						}
					} else {
						$res = $mi;
					}
				}
				break;
			case 'move_down':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$res = $mi->moveDown($bm);
						$type = ($mi->getAreaID() == $bm->getAVAreaID()) ? "av" : "photos";
						if (!db::isError($res)) {
							header('Location: band_member_media.php?id=' . $_GET['id'] . '&type=' . $type);
						}
					} else {
						$res = $mi;
					}
				}
				break;
		}
	}
	
	$page_title = 'Band Member Media';
	include('layout/header.php');

?>

<?	if (!db::isError($bm)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="band.php">Manage&nbsp;Band&nbsp;&#62;</a>&nbsp;<a href="band_members.php">Band Members&nbsp;&#62;</a>&nbsp;<a href="band_member_edit.php?id=<?=$_GET['id']?>">View/Edit Band Member&nbsp;&#62;</a>&nbsp;Media
</div>



<?	
	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}
	
	if (is_object($ma) && !db::isError($ma)) {
	
		$exitURL = "band_member_edit.php?id=" . $_GET['id'];
		$exitText = "Return to Band Member";
		$canEdit = $bm->canEdit();
		if ($bm->canEdit()) {
			$addURL = "band_member_media_add.php?id=" . $_GET['id'] . '&type=' . $type;
		}
		$editURL = "band_member_media_edit.php?id={$_GET['id']}&amp;";
		if ($ma->getDisplay() == 'grid') {
			include('layout/media_grid.php'); 
		} else {
			include('layout/media_list.php');
		}
	} else if (User::isAdmin()) {
	
		$ma->outputDialog('Register Band Member', $PHP_SELF . '?task=register_media&amp;id=' . $_GET['id'] . '&amp;type=' . $type);
	
	} else {
	
		$ma->outputDialog('Return to Band Member','band_member_edit.php?id=' . $_GET['id']);
		
	 } ?>


<? } else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="band.php">Manage&nbsp;Band&nbsp;&#62;</a>&nbsp;<a href="band_members.php">Band Members&nbsp;&#62;</a>&nbsp;Invalid Entry
</div>

<?=$bm->outputDialog('Return to Band Members','band_members.php', 'Please specify a valid band member ID.');?>

<? } ?>

<? include('layout/footer.php'); ?>
